package chu.dejian.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;

import com.sam.blemodule.R;
import com.sam.ui.base.activity.BaseActivity;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.adapter.AttentionMessageAdapter;
import chu.dejian.db.DBHelper;
import chu.dejian.entity.MeasureMessageEntity;

public class MeasureMessageActivity extends BaseActivity{
	private static MeasureMessageActivity instance;
	private LeftImgTitleRightImgBar header;
	private ListView attentionMessageListView;
	private AttentionMessageAdapter attentionMessageAdapter;
	private List<MeasureMessageEntity> measureMessageList;

	public static final String KEY_ATTENDEE = "attendee";
	public static final String KEY_ATTENDEE_NAME = "attendee_name";

	@Override
	protected void onCreate(@Nullable Intent savedInstanceStateIntent) {
		setContentView(R.layout.fragment_measure_message);
	}

	public void findView() {
		header = (LeftImgTitleRightImgBar) findViewById(R.id.header);
		attentionMessageListView = (ListView) findViewById(R.id.attention_list);
	}

	public void initView() {
		EditText searchET = (EditText)findViewById(R.id.search_et);
		searchET.addTextChangedListener(textWatcher);
		measureMessageList = new ArrayList<>();
		attentionMessageAdapter = new AttentionMessageAdapter(MeasureMessageActivity.this,measureMessageList);
		attentionMessageListView.setAdapter(attentionMessageAdapter);
	}

	@Override
	public void onResume() {
		super.onResume();
		if(getIntent() == null) return;
		String attendee = getIntent().getStringExtra(KEY_ATTENDEE);
		String attendeeName = getIntent().getStringExtra(KEY_ATTENDEE_NAME);
		if(attendeeName == null) return;
		header.setTitleText(attendeeName);
		List<MeasureMessageEntity> messages = DBHelper.getInstance().getMessageDao().getAttendeeMessages(attendee);
//		List<MeasureMessageEntity> messages = DBHelper.getInstance().getMessageDao().getAllMessage();
		if(messages == null || messages.size() == 0) return;
		measureMessageList.clear();
		if(messages != null) measureMessageList.addAll(messages);
		attentionMessageAdapter.notifyDataSetChanged();
	}

	private TextWatcher textWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			attentionMessageAdapter.getFilter().filter(s);
		}

		@Override
		public void afterTextChanged(Editable s) {

		}
	};

}
